package com.example.zhihuigongdi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.zhihuigongdi.commer.CommonException;
import com.example.zhihuigongdi.entity.Personnel;
import com.example.zhihuigongdi.entity.User;
import com.example.zhihuigongdi.entity.respon.Result;
import com.example.zhihuigongdi.mapper.PersonnelMapper;
import com.example.zhihuigongdi.service.IPersonnelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.zhihuigongdi.utils.CheckUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liuhongbin
 * @since 2024-04-17
 */
@Service
public class PersonnelServiceImpl extends ServiceImpl<PersonnelMapper, Personnel> implements IPersonnelService {

    @Autowired
    private CheckUtil checkUtil;

    @Autowired
    @Lazy
    private IPersonnelService personnelService;
    @Override
    public Result addPersonel(Personnel personnel) {
        User user = checkUtil.getUser();
        checkUtil.checkUser(user);

        if (!user.getUserRole().equals("admin")){
            throw new CommonException("权限不足");
        }

        QueryWrapper<Personnel> personnelQueryWrapper = new QueryWrapper<>();
        personnelQueryWrapper.eq("employee_id", personnel.getEmployeeId());
        Personnel one = personnelService.getOne(personnelQueryWrapper);
        if (BeanUtil.isNotEmpty(one)){
            throw new CommonException("重复职工号");
        }

        personnelService.save(personnel);

        return Result.success("添加成功");
    }

    @Override
    public Result removePersonnel(int employeeId) {
        User user = checkUtil.getUser();
        checkUtil.checkUser(user);

        if (!user.getUserRole().equals("admin")){
            throw new CommonException("权限不足");
        }
        QueryWrapper<Personnel> personnelQueryWrapper = new QueryWrapper<>();
        personnelQueryWrapper.eq("employee_id", employeeId);
        Personnel one = personnelService.getOne(personnelQueryWrapper);
        if (BeanUtil.isEmpty(one)){
            throw new CommonException("无此员工");
        }

        personnelService.remove(personnelQueryWrapper);

        return Result.success("删除成功");
    }

    @Override
    public Result getPersonnels(int current,int size) {
        User user = checkUtil.getUser();
        checkUtil.checkUser(user);

        if (!user.getUserRole().equals("admin")){
            throw new CommonException("权限不足");
        }
        PageHelper.startPage(current,size);
        List<Personnel> list = personnelService.list();
        PageInfo<Personnel> personnelPageInfo = new PageInfo<>(list);
        return Result.success(personnelPageInfo);
    }
}
